//
//  MTGiftCountTableView.swift
//  MT
//
//  Created by M on 2019/12/31.
//  Copyright © 2019 张强. All rights reserved.
//

import UIKit
import MTCategory

@objc
protocol MTGiftCountTableViewDelegate: NSObjectProtocol {
    @objc optional func giftCountTableView(_ tableView: MTGiftCountTableView, didSelectCountWithCount count: Int)
}

class MTGiftCountTableView: UITableView {
    
    weak var countDelegate: MTGiftCountTableViewDelegate?
    
    /// 数量类型
    private static var giftCountTypes: [GiftCountType] {
        [._1, ._5, ._10, ._66, ._100]
    }

    static var tableViewHeight: CGFloat {
        CGFloat(giftCountTypes.count) * cellHeight
    }
    
    static var cellHeight: CGFloat {
        32.0
    }

    @objc
    enum GiftCountType: Int {
        case _1     = 1
        case _5     = 5
        case _10    = 10
        case _66    = 66
        case _100   = 100
        
        var name: String {
            switch self {
            case ._1:
                return "一心一意"
            case ._5:
                return "屋里藏娇"
            case ._10:
                return "十全十美"
            case ._66:
                return "顺心顺意"
            case ._100:
                return "喜欢百分百"
            }
        }
    }
    
    init(frame: CGRect) {
        super.init(frame: frame, style: .grouped)
        setup()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

// MARK: ------------------------------------ 初始化 ----------------------------------------
extension MTGiftCountTableView {
    
    private func setup() {
        delegate = self
        dataSource = self
        backgroundColor = .white
        layer.cornerRadius = 4
        isScrollEnabled = false
//        separatorColor = UIColor(hex: 0xafa2a4)
//        separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        showsVerticalScrollIndicator = false
        register(MTGiftCountTableViewCell.self, forCellReuseIdentifier: String(describing: MTGiftCountTableViewCell.self))
    }
    
}

// MARK: ------------------------------------ 所有代理 ----------------------------------------
// MARK: ------------------------------------ UITableViewDelegate ----------------------------------------
extension MTGiftCountTableView: UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let delegate = countDelegate {
            delegate.giftCountTableView?(self, didSelectCountWithCount: MTGiftCountTableView.giftCountTypes[indexPath.row].rawValue)
        }
    }
    
}

// MARK: ------------------------------------ UITableViewDataSource ----------------------------------------
extension MTGiftCountTableView: UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        MTGiftCountTableView.giftCountTypes.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        MTGiftCountTableView.cellHeight
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let type = MTGiftCountTableView.giftCountTypes[indexPath.row]
        guard let c = tableView.dequeueReusableCell(withIdentifier: String(describing: MTGiftCountTableViewCell.self), for: indexPath) as? MTGiftCountTableViewCell else {
            return tableView.dequeueReusableCell(withIdentifier: String(describing: MTGiftCountTableViewCell.self), for: indexPath)
        }
        c.model = type
        c.underline.isHidden = indexPath.row == MTGiftCountTableView.giftCountTypes.count - 1
        return c
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        CGFloat.leastNonzeroMagnitude
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        CGFloat.leastNonzeroMagnitude
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        nil
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        nil
    }
    
}

// MARK: ------------------------------------ MTGiftCountTableViewCell ----------------------------------------
class MTGiftCountTableViewCell: UITableViewCell {
    
    var model: MTGiftCountTableView.GiftCountType? {
        didSet {
            guard let model = model else { return }
            countLabel.text = "\(model.rawValue)"
            titleLabel.text = model.name
        }
    }
    
    /// 数字
    lazy var countLabel: UILabel = {
        let label = UILabel()
        label.font = PingFangSCSemiboldFont(12)
        label.textColor = UIColor(hex: 0x161616)
        label.textAlignment = .left
        return label
    }()
    
    /// Title
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = PingFangSCRegularFont(12)
        label.textColor = UIColor(hex: 0x5d5d5d)
        label.textAlignment = .left
        return label
    }()
    
    /// underline
    lazy var underline: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex: 0xafa2a4)
        return view
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        initializeUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

// MARK: ------------------------------------ 初始化 ----------------------------------------
extension MTGiftCountTableViewCell {
    
    private func initializeUI() {
        contentView.backgroundColor = .white
        contentView.addSubview(countLabel)
        contentView.addSubview(titleLabel)
        contentView.addSubview(underline)
        
        countLabel.snp.makeConstraints { (make) in
            make.left.equalTo(contentView).offset(17)
            make.centerY.equalTo(contentView)
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(countLabel)
            make.left.equalTo(contentView.snp.centerX).offset(-10)
        }
        
        underline.snp.makeConstraints { (make) in
            make.bottom.equalTo(contentView)
            make.height.equalTo(1)
            make.left.right.equalTo(contentView)
        }
    }
    
}
